import 'package:flutter/material.dart';
import 'package:znmm/config/app_style.dart';

class CounterButton extends StatefulWidget {
  final Function(int iQty) onIncrementSelected;
  final Function(int iQty) onDecrementSelected;
  final Function()? onCallback;

  final int iANumber;
  final Axis orientation;

  const CounterButton({
    Key? key,
    required this.onIncrementSelected,
    required this.onDecrementSelected,
    this.iANumber = 0,
    this.orientation = Axis.horizontal,
    this.onCallback,
  }) : super(key: key);

  @override
  _CounterButtonState createState() => _CounterButtonState();
}

class _CounterButtonState extends State<CounterButton> {
  int _label = 0;

  @override
  Widget build(BuildContext context) {
    return widget.orientation == Axis.horizontal
        ? Row(mainAxisAlignment: MainAxisAlignment.end, children: _buildBody())
        : Column(children: _buildBody().reversed.toList());
  }

  List<Widget> _buildBody() {
    return [
      _button(const Icon(Icons.remove), () {
        if (_label > 0) {
          setState(() {
            _label--; // 减少操作
          });
          widget.onDecrementSelected(_label); // 调用传入的减少回调函数
        }
      }),
      Padding(
        padding: const EdgeInsets.symmetric(horizontal: 10),
        child: Text("$_label", style: h2Style.copyWith(fontSize: 15)),
      ),
      _button(const Icon(Icons.add), () {
        // if (widget.iANumber > 0) {
        // } else if (widget.onCallback != null) {
        //   widget.onCallback!();
        // }

        setState(() {
          _label++; // 增加操作
        });
        widget.onIncrementSelected(_label); // 调用传入的增加回调函数
      }),
    ];
  }

  Widget _button(Icon icon, Function() onTap) {
    return RawMaterialButton(
      constraints: BoxConstraints.tight(const Size(36, 36)),
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
      fillColor: Colors.white,
      onPressed: onTap,
      child: icon,
    );
  }
}


/*


     CounterButton(
                    label: widget.product.quantity,
                    onIncrementSelected: () {},
                    onDecrementSelected: () {},
                  ).fadeAnimation(0.8)
*/